<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2019/2/22
 * Time: 16:17
 */

namespace app\login\service;


use app\login\model\Account;
use app\login\model\Popularize;
use app\login\model\Registered;

class StatisticsService
{
    # 获取一个月内所有的流量记录
    public function monthData($from, $time, $model_str)
    {
        $monthData = $this->getOneMonthData($from, $time, $model_str);
        $result = $this->timeMonthClassification($monthData);
        return $result;
    }
    # 获取一周内每个标识码的推广数量
    public function weekSign($from, $time, $model_str)
    {
        $weekData = $this->getWeekData($from, $time, $model_str);
        $sign_arr = $this->getSignArr($from);
        $result = $this->timeWeekClassification($weekData, $sign_arr);
        $new_sign_arr = $this->withSignArr($result);
        return $new_sign_arr;
    }

    /**
     * @param $from 推广方名字
     * @return array 一个内当前推广方所有数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getOneMonthData($from,$time, $model_str)
    {
        $model = 'app\\login\\model\\'.$model_str;
        if($model_str == 'Popularize'){
            $model_object = new $model;
            return $model_object->where('from','=',$from)
                ->order($time,'desc')
                ->whereTime($time, 'm')
                ->select()
                ->toArray();
        }
        if($model_str == 'Registered'){
            $model_object = new $model;
            return $model_object->where('from','=',$from)
                ->where('first', '=', 1)
                ->order($time,'desc')
                ->whereTime($time, 'm')
                ->select()
                ->toArray();
        }
    }

    /**
     * @param $from 推广方名字
     * @param $time MySQL的一个字段
     * @param $model_str 动态新建模型
     * @return mixed
     */
    private function getWeekData($from,$time, $model_str)
    {
        $model = 'app\\login\\model\\'.$model_str;
        if($model_str == 'Popularize'){
            $model_object = new $model;
            return $model_object->where('from','=',$from)
                ->order($time,'desc')
                ->whereTime($time, 'w')
                ->select()
                ->toArray();
        }
        if($model_str == 'Registered'){
            $model_object = new $model;
            return $model_object->where('from','=',$from)
                ->where('first', '=', 1)
                ->order($time,'desc')
                ->whereTime($time, 'w')
                ->select()
                ->toArray();
        }
    }
    # 按照时间来分类统计
    private function timeMonthClassification($monthData)
    {
        $today = strtotime(date("Y-m-d"),time());
        $yesterday = strtotime("yesterday");
        $lastWeek = strtotime("last week");
        $lastMonth = strtotime("-1 month");

        $num_d = [
            'today'  => 0,
            'yesterday' => 0,
            'lastWeek' => 0,
            'lastMonth' => 0
        ];

        $num_f = [
            'today'  => 0,
            'yesterday' => 0,
            'lastWeek' => 0,
            'lastMonth' => 0
        ];

        foreach ($monthData as $value){
            $add_time = strtotime($value['add_time']);
            if($today<= $add_time){
                # 计算今天的
                if($value['type'] == 'd'){
                    $num_d['today'] += 1;
                }
                if($value['type'] == 'f'){
                    $num_f['today'] += 1;
                }
            }
            if($add_time <= $today && $add_time >= $yesterday){
                # 计算昨天的
                if($value['type'] == 'd'){
                    $num_d['yesterday'] += 1;
                }
                if($value['type'] == 'f'){
                    $num_f['yesterday'] += 1;
                }
            }
            if($add_time > $lastWeek){
                # 计算过去一周
                if($value['type'] == 'd'){
                    $num_d['lastWeek'] += 1;
                }
                if($value['type'] == 'f'){
                    $num_f['lastWeek'] += 1;
                }
            }
            if($add_time > $lastMonth){
                # 计算过去一月
                if($value['type'] == 'd'){
                    $num_d['lastMonth'] += 1;
                }
                if($value['type'] == 'f'){
                    $num_f['lastMonth'] += 1;
                }
            }
        }

        return [
            'd' => $num_d,
            'f' => $num_f
        ];
    }

    # 按照每个标识码的一周内数量进行统计
    private function timeWeekClassification($weekData, $sign_arr)
    {
        $today = strtotime(date("Y-m-d"),time());
        $yesterday = strtotime("yesterday");
        $lastWeek = strtotime("last week");
        $array_keys = array_keys($sign_arr);
        # 循环当前推广方一周内的所有记录
        foreach ($weekData as $value){
            $add_time = strtotime($value['add_time']);
            foreach ($array_keys as $key_data){
                if($today<= $add_time){
                    # 计算今天的
                    if($value['sign'] == $key_data){
                        $sign_arr[$value['sign']]['today'] += 1;
                    }
                }
                if($add_time <= $today && $add_time >= $yesterday){
                    # 计算昨天的
                    if($value['sign'] == $key_data){
                        $sign_arr[$value['sign']]['yesterday'] += 1;
                    }
                }
                if($add_time > $lastWeek){
                    # 计算过去一周
                    if($value['sign'] == $key_data) {
                        $sign_arr[$value['sign']]['lastWeek'] += 1;
                    }
                }
            }
        }
        return $sign_arr;
    }
    # 循环sign数组组成新的数组 新的数组key为sign  value为一个数组
    private function getSignArr($from)
    {
        $model = new Account();
        $sign_str = $model->where('name', '=',$from)
            ->value('sign');
        $sign_arr = explode('%',$sign_str);
        $new_arr = [];
        foreach($sign_arr as $value){
            $new_arr[$value] = [
                'today'  => 0,
                'yesterday' => 0,
                'lastWeek' => 0,
            ];
        }
        return $new_arr;
    }
    #
    private function withSignArr($sign_arr)
    {
        $new_sign_arr = [];
        foreach ($sign_arr as $key=>$value){
            $arr = $value;
            $arr['sign'] = $key;
            array_push($new_sign_arr,$arr);
        }
        return $new_sign_arr;
    }
}